package com.ccx.coachsite.ui.softmgr;

import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.ccx.coachsite.R;
import com.ccx.coachsite.model.SoftShareMessage;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.customview.LazyViewPager;
import com.ccx.coachsite.ui.customview.ViewPagerTab;


public class SoftMgrTabLazyView extends CommonAppView {
	
	public static final String TAG = "SoftMgrTabLazyView";

	private Context ctx;
	
	public LazyViewPager vp;
	public ViewPagerTab tab;
	
	private SoftMgrTopicView softMgrTopicView;	
	private SoftMgrSoftListView softMgrAppView;
	private SoftMgrSoftListView softMgrGameView;
	public SoftMgrSearchView softMgrSearchView;
	
	
	//点击Item后把专题结果展示在此View
	private SoftMgrTopicWebView topicDetailListResultView = null; 
	
	private final int[] names = { R.string.softmgr_view_tab_topic, R.string.softmgr_view_tab_app, 
			R.string.softmgr_view_tab_game, R.string.softmgr_view_tab_search };
	
	public int initTab = 0;  //默认加载的Tab序号
	
	public SoftMgrTabLazyView(Context paramContext) {	
		super(paramContext);

		ctx = paramContext;
		
		addView(R.layout.soft_mgr_soft_viewpager);
		
		vp = (LazyViewPager) findViewById(R.id.pager);
		vp.setInitTab(initTab);
		vp.addView(createSoftMgrTopicView());
		vp.addView(createSoftMgrAppView());
		vp.addView(createSoftMgrGameView());
		vp.addView(createSoftMgrSearchView());
		
		tab = (ViewPagerTab) findViewById(R.id.pagertab);
		tab.setInitTab(initTab);
		tab.addTitle(new String[]{
				paramContext.getResources().getString(names[0]),
				paramContext.getResources().getString(names[1]),
				paramContext.getResources().getString(names[2]),
				paramContext.getResources().getString(names[3])});
		tab.setViewpager(vp);
		vp.setTab(tab);
	}
	
	public SoftMgrTabLazyView(Context paramContext,int defaultTab) {		
		super(paramContext);
				
		ctx = paramContext;
		
		initTab = defaultTab;
		
		addView(R.layout.soft_mgr_soft_viewpager);
		
		vp = (LazyViewPager) findViewById(R.id.pager);
		vp.setInitTab(initTab);
		vp.addView(createSoftMgrTopicView());
		vp.addView(createSoftMgrAppView());
		vp.addView(createSoftMgrGameView());
		vp.addView(createSoftMgrSearchView());
		
		tab = (ViewPagerTab) findViewById(R.id.pagertab);
		tab.setInitTab(initTab);
		tab.addTitle(new String[]{
				paramContext.getResources().getString(names[0]),
				paramContext.getResources().getString(names[1]),
				paramContext.getResources().getString(names[2]),
				paramContext.getResources().getString(names[3])});
		tab.setViewpager(vp);
		vp.setTab(tab);
	}

	public void addView(int paramInt) {
		LayoutInflater.from(getContext()).inflate(paramInt, this);
	}
	
	@Override
	public void clearCache() {
		super.clearCache();
		if (softMgrTopicView!=null)
			softMgrTopicView.clearCache();
		if (softMgrAppView!=null)
			softMgrAppView.clearCache();
		if (softMgrGameView!=null)
			softMgrGameView.clearCache();
		if (softMgrSearchView!=null)
			softMgrSearchView.clearCache();
	}
	
	private View createSoftMgrTopicView(){
		
		if ( softMgrTopicView != null ) 
			return softMgrTopicView;
		
		softMgrTopicView = new SoftMgrTopicView(ctx);		
		softMgrTopicView.softMgrTabLazyView = this;

		return softMgrTopicView;
	}
	
	private View createSoftMgrAppView(){
		
		if ( softMgrAppView != null ) 
			return softMgrAppView;
		
		HashMap<String, Object> initParaMap = new HashMap<String, Object>();
		initParaMap.put(SoftMgrSoftListView.EXTRA_LIST_TYPE, SoftMgrSoftListView.TYPE_APP_LIST);
		initParaMap.put(SoftMgrSoftListView.EXTRA_SHARE_TYPE, SoftShareMessage.TYPE_APP_SHARE_TYPE);
		softMgrAppView = new SoftMgrSoftListView(ctx, initParaMap);

		return softMgrAppView;
	}
	
	private View createSoftMgrGameView(){
		
		if ( softMgrGameView != null ) 
			return softMgrGameView;
		
		HashMap<String, Object> initParaMap = new HashMap<String, Object>();
		initParaMap.put(SoftMgrSoftListView.EXTRA_LIST_TYPE, SoftMgrSoftListView.TYPE_APP_LIST);
		initParaMap.put(SoftMgrSoftListView.EXTRA_SHARE_TYPE, SoftShareMessage.TYPE_GAME_SHARE_TYPE);
		softMgrGameView = new SoftMgrSoftListView(ctx, initParaMap);
		
		return softMgrGameView;
	}
	
	private View createSoftMgrSearchView(){
		
		if ( softMgrSearchView != null ) 
			return softMgrSearchView;
		
		softMgrSearchView = new SoftMgrSearchView(ctx);	

		return softMgrSearchView;
	}
	
	@Override
	public boolean flushView() {
		
		return goBack();
	}
	
	@Override
	public boolean goBack() {
		
		if ( removeResultView() ) 
			return true;
		
		//获取当前View
		if (tab.selectTab==0) {
			if (softMgrTopicView!=null)
				return softMgrTopicView.flushView();
		}
		
		if (tab.selectTab==1) {
			
		}
		
		if (tab.selectTab==2) {
			
		}
		
		if (tab.selectTab==3) {
			if (softMgrSearchView!=null)
				return softMgrSearchView.flushView();
		}
		
		return super.flushView();
	}
	
	public void addResultView(SoftMgrTopicWebView topicDetailView){
		
		if (topicDetailView==null) return;
		topicDetailListResultView = topicDetailView;
		topicDetailListResultView.parentView = this;
		this.addView(topicDetailListResultView);
	}
	
	/**
	 * 返回true表示移除了View、消化处理了back键
	 * @param resultsView
	 * @return
	 */
	public boolean removeResultView(){
		
		if ( topicDetailListResultView!=null && topicDetailListResultView.isShown() ){		
			topicDetailListResultView.clearCache();
			this.removeView(topicDetailListResultView);
			topicDetailListResultView.parentView = null;
			topicDetailListResultView = null;
			
			return true;
		}
		return false;
	}
}


